﻿using System;
using System.Linq;
using System.Windows.Forms;

using DevExpress.XtraEditors;
using DevExpress.XtraSplashScreen;
using Sumen.Business;
using Sumen.Business.Presenter.Category;
using Sumen.Business.Presenter.Sys;
using Sumen.Library.Classes.Common;
using Sumen.Library.Enum;
using Sumen.Library.Froms;
using Sumen.Model.Model;


namespace Sumen.GUI.Category
{
    public partial class Frm_Product_Category_Edit : XtraForm
    {
        Frm_Category_Description frm_des;
        private CategoryPresenter _categoryPresenter;
        private EnumFormStatus _enumFormStatus;
        private category _categoryInfo;
        private int _categoryId;
        private EnumFormCode _fromCode = EnumFormCode.FrmCategory;

        public Frm_Product_Category_Edit()
        {
            InitializeComponent();
            _categoryPresenter = new CategoryPresenter();
            cmbStatus.Properties.DataSource = Enum.GetValues(typeof(EnumStatusAccess)).Cast<EnumStatusAccess>().Select(v => v.ToString()).ToList();
        }

        private void Frm_Product_Category_Edit_Load(object sender, EventArgs e)
        {
            LoadForm();
        }


        public EnumFormStatus EnumStatus
        {
            get { return _enumFormStatus; }
            set
            {
                _enumFormStatus = value;
            }
        }

        private void LoadForm()
        {
            if (_enumFormStatus == EnumFormStatus.Add)
            {
                txtTitle.EditValue = null;
                txtCode.EditValue = null;
                txtCode.Properties.ReadOnly = false;
                txtAlias.EditValue = null;
                txtPath.EditValue = null;
                cmbStatus.ItemIndex = 0;
                chkFeatured.CheckState = CheckState.Checked;
                //popupTreeDM.PrimaryKey = null;
                txtMetaDes.EditValue = null;
                txtMetaKey.EditValue = null;
                chkActived.CheckState = CheckState.Checked;
            }
            else if (_enumFormStatus == EnumFormStatus.Modify)
            {
                _categoryInfo = _categoryPresenter.GetById(CategoryId);
                txtTitle.EditValue = _categoryInfo.Cat_Title;
                txtCode.EditValue = _categoryInfo.Cat_Code;
                txtCode.Properties.ReadOnly = true;
                txtAlias.EditValue = _categoryInfo.Cat_Alias;
                txtPath.EditValue = _categoryInfo.Cat_Path;
                cmbStatus.ItemIndex = Convert.ToInt32(_categoryInfo.Cat_Published);
                chkFeatured.CheckState = _categoryInfo.Cat_Featured == 1 ? CheckState.Checked : CheckState.Unchecked;
                popupTreeDM.PrimaryKey = _categoryInfo.Cat_Parent_id;
                txtMetaDes.EditValue = _categoryInfo.Cat_MetaDesc;
                txtMetaKey.EditValue = _categoryInfo.Cat_MeteKey;
                chkActived.CheckState = _categoryInfo.Cat_Actived == 1 ? CheckState.Checked : CheckState.Unchecked;
            }
            popupTreeDM.ReLoad();
        }

        public int CategoryId
        {
            get { return _categoryId; }
            set { _categoryId = value; }
        }


        private void btnMoTa_Click(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            if (frm_des == null)
            {
                frm_des = new Frm_Category_Description();
                SplashScreenManager.CloseForm();
                frm_des.ShowDialog();
            }
            else
            {
                SplashScreenManager.CloseForm();
                frm_des.ShowDialog();
            }
            
        }

        private void buttonsArray1_btnEventUpdate_click(object sender, EventArgs e)
        {
            if (Validation())
            {
                if (_enumFormStatus == EnumFormStatus.Add)
                {
                    category newCategory = Add();
                    if (_categoryPresenter.Add(newCategory) == 1)
                    {
                        _enumFormStatus = EnumFormStatus.Modify;
                        CategoryId = newCategory.Cat_ID;
                        ClsMsgBox.ThongTin("Thêm mới thành công");
                        if (ClsPublic.SYSTEM_WRITELOG == "1")
                        {
                            SysLogPresenter log = new SysLogPresenter();
                            log.Add(_fromCode, EnumFormStatus.Add, newCategory.Cat_Title, "");
                        }
                        this.Close();
                    }
                }
                else if (_enumFormStatus == EnumFormStatus.Modify)
                {
                    if (CategoryId.ToString() != popupTreeDM.PrimaryKey.ToString())
                    {
                        category newCategory = Add();
                        int count = _categoryPresenter.Update(newCategory, CategoryId);
                        if (count == 1)
                        {
                            ClsMsgBox.ThongTin("Cập nhật thành công");
                            if (ClsPublic.SYSTEM_WRITELOG == "1")
                            {
                                SysLogPresenter log = new SysLogPresenter();
                                log.Add(_fromCode, EnumFormStatus.Modify, newCategory.Cat_Title, "");
                            }
                            this.Close();
                        }
                    }
                    else
                    {
                        ClsMsgBox.CanhBao("Không thể đặt danh mục cha bởi chính nó.");
                    }

                }
            }
        }

        private bool Validation()
        {
            dxErrorProvider1.ClearErrors();
            if ((string)txtTitle.EditValue == null || txtTitle.EditValue == "")
            {
                dxErrorProvider1.SetError(txtTitle, "Vui lòng nhập");
                return false;
            }
            //else if (txtAlias.EditValue == null || txtAlias.EditValue == "")
            //{
            //    dxErrorProvider1.SetError(txtAlias, "Vui lòng nhập");
            //    return false;
            //}
            else if (string.IsNullOrEmpty((string)txtCode.EditValue))
            {
                dxErrorProvider1.SetError(txtCode, "Vui lòng nhập");
                return false;
            }
            return true;
        }

        private category Add()
        {
            category newCat = new category();
            newCat.Cat_Title = txtTitle.EditValue + string.Empty;
            newCat.Cat_Code = txtCode.EditValue + string.Empty;
            newCat.Cat_Alias = txtAlias.EditValue + string.Empty;
            newCat.Cat_Path = txtPath.EditValue + string.Empty;
            newCat.Cat_Published = Convert.ToSByte(cmbStatus.ItemIndex);
            newCat.Cat_Featured = Convert.ToInt16(chkFeatured.EditValue);
            newCat.Cat_Parent_id = Convert.ToInt32(popupTreeDM.PrimaryKey);
            newCat.Cat_MetaDesc = txtMetaDes.EditValue + string.Empty;
            newCat.Cat_MeteKey = txtMetaKey.EditValue + string.Empty;
            newCat.Cat_Actived = chkActived.CheckState == CheckState.Checked ? 1 : 0;
            return newCat;
        }

        private void buttonsArray1_btnEventClose_click(object sender, EventArgs e)
        {
            this.Close();
        }




    }
}